<?php

namespace App\Http\Controllers\Home;

use App\Model\lamp_district;
use App\Model\writer;
use App\Model\writer_detail;
use App\Model\writer_details;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class userVIPController extends Controller
{

    public function show(Request $request, $id)
    {
        if($request->isMethod('post')){
            $edit = writer::find($id);
            $rules = array(
                'nickname'=>'required|between:3,8',
                'tel'=>'required|digits:11',
                'email'=>'required|email|unique:readusers,email',
                'qq'=>'required|numeric|',
                'newaddress'=>'required',
                'icon'=>'image'
            );
            $mess = array(
                'nickname.required'=>'笔名不能为空',
                'nickname.between'=>'笔名要介于3到8位之间',
                'tel.required'=>'手机号不能为空',
                'tel.digits'=>'手机号必须为11位',
                'qq.required'=>'qq不能为空',
                'qq.numeric'=>'qq必须为数字',
                'email.unique'=>'邮箱已被注册',
                'email.required'=>'邮箱不能为空',
                'email.email'=>'邮箱格式不正确',
                'newaddress.required'=>'地址不能为空',
                'icon.image'=>'上传文件必须为图片'
            );
            $this->validate($request,$rules,$mess);
            $edit->nickname = $request->nickname;
            $edit->tel = $request->tel;
            $edit->qq = $request->qq;
            $edit->email = $request->email;
            $edit->sex = $request->sex;
            $edit->regtime=time();
            $edit->newaddress = $request->newaddress;
            $id = $edit->id;
            if($request->icon){
                $name = $id.'.jpg';
                $dir = public_path().'\image\writer';
                $request->icon->move(public_path().'/image/writer',$name);
                $upnew = writer::find($id);
                $upnew->icon = $name;
                $upnew->save();
            }
            $edit->save();

            return redirect('huweize/home/userVIP/'.$id);
        }
        $userList = writer::find($id);
        $List = writer_detail::where('wid',$id)->get();
        return view('huweize.home.userVIP',compact('userList','List','id'));
    }

    public function Update(Request $request,$id)
    {
           $add=writer_detail::where('wid',$id)->get();
           if (!count($add)){
               $new=new writer_detail();
               $rules = array(
                 'age'=>'numeric'
               );
               $mess = array(
                 'age.numeric'=>'年龄只能填写数字哦'
               );
               $this->validate($request,$rules,$mess);
               $new->wid=$id;
               $new->name=$request->name;
               $new->job=$request->job;
               $new->age=$request->age;
               $new->school=$request->school;
               $new->record=$request->record;
               $new->discipline=$request->discipline;
               $new->save();

               return redirect('huweize/home/userVIP/'.$id);
           }else{
               $wid=$request->wid;
               $edit = writer_detail::find($id);
               $edit->name = $request->name;
               $edit->job = $request->job;
               $edit->age = $request->age;
               $edit->school = $request->school;
               $edit->record = $request->record;
               $edit->discipline = $request->discipline;
               $edit->save();
               return redirect('huweize/home/userVIP/'.$wid);
           }
    }

    public function lis(Request $request)
    {
          $upid=$request->upid;

          $data=lamp_district::where('upid',$upid)->get();

          return json_decode($data);
    }
}
